<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller\Test;

use App\Model\Entity\User;
use Exception;
use Swoft\Co;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Redis\Redis;
use Swoft\Log\Helper\CLog;
use Swoole\Coroutine\Http\Client;
use Throwable;
use function random_int;

/**
 * https://www.wenjiangs.com/doc/zzepmghc 【协程文档】
 *
 * Class SwoftCoController
 *
 * @since 2.0
 *
 * @Controller(prefix="Co")
 */
class SwoftCoController
{

    /**
     * @RequestMapping(route="test_multi",method={RequestMethod::GET})
     *
     * @return array
     *
     * @throws Exception
     */
    public function testMulti()
    {
        $requests = [
//            'method'       => [$this, 'requestMethod'],
            'staticMethod' => "App\Http\Controller\Test\SwoftCoController::requestMehtodByStatic",
            'closure'      => function () {
                $cli = new Client('www.baidu.com', 80);
                $cli->get('/');
                $result = $cli->body;
                $cli->close();
                return $result;
            }
        ];
        return Co::multi($requests);
    }
    /**
     * @return mixed
     */
    public function requestMethod()
    {
        $cli = new Client('www.baidu.com', 80);
        $cli->get('/');
        $result = $cli->body;
        $cli->close();
        return $result;
    }
    /**
     * @return mixed
     */
    public static function requestMehtodByStatic()
    {
        $cli = new Client('www.baidu.com', 80);
        $cli->get('/');
        $result = $cli->body;
        $cli->close();
        return $result;
    }

    /**
     * @RequestMapping(route="create",method={RequestMethod::GET})
     *
     * @return array
     *
     * @throws Exception
     */
    public function create()
    {
        Co::create(function(){  //sgo
            Co::sleep(10);
            for($i=0;$i<10;$i++){
                CLog::info("hello");
            }
        });

        Co::create(function(){
            Co::sleep(20);
            vdump("world");
        });

        $id = Co::id(); //获取当前协程 ID，-1 为非协程环境：
        vdump('当前协程ID:'.$id);
        $tid = Co::tid(); //获取顶级（最外层）协程 ID：
        vdump('顶级协程ID:'.$tid);
        return "你好";
    }

    /**
     * @RequestMapping(route="sgo",method={RequestMethod::GET})
     *
     * @return array
     *
     * @throws Exception
     */
    public function sgo()
    {
        srun(function(){
            sgo(function(){
                for($i=0;$i<10;$i++){
                    Co::sleep(1);
                    CLog::info("hello");
                }
            });

            sgo(function(){
                for($i=0;$i<10;$i++){
                    Co::sleep(1);
                    CLog::warning("hello");
                }
            });
            return true;
        });

        $id = Co::id(); //获取当前协程 ID，-1 为非协程环境：
        vdump('当前协程ID:'.$id);
        $tid = Co::tid(); //获取顶级（最外层）协程 ID：
        vdump('顶级协程ID:'.$tid);
        return "你好";
    }
}
